package com.iot.manage.controller;

import com.alibaba.fastjson.JSONObject;
import com.iot.manage.pojo.DeviceEvent;
import com.iot.manage.pojo.SysCode;
import com.iot.manage.result.Code;
import com.iot.manage.result.Result;
import com.iot.manage.service.DeviceEventService;
import com.iot.manage.service.TokenService;
import com.iot.manage.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 设备事件管理控制层
 *
 * @author zhuzhi
 */
@RestController
@RequestMapping("/api/private/v1")
public class DeviceEventController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private DeviceEventService deviceEventService;

    /**
     * 查询设备事件信息列表
     *
     * @param token
     * @param query 模糊查询关键字
     * @param pagenum 当前页
     * @param pagesize 每页条数
     * @return
     */
    @GetMapping("/findDEventList")
    public Result findDEventList(
            @RequestHeader("token") String token,
            @RequestParam("query") String query,
            @RequestParam("pagenum") int pagenum,
            @RequestParam("pagesize") int pagesize) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<DeviceEvent> deviceEventList;
        int deviceEventTotal;
        try{
            deviceEventList = deviceEventService.findDEventList(query,pagenum,pagesize);
            deviceEventTotal = deviceEventService.countDEventList(query);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",deviceEventTotal);
        map.put("pagenum",pagenum);
        map.put("deviceEventList",deviceEventList);
        return result.build(map, Code.S200);
    }

    /**
     * 新增设备事件信息
     *
     * @param token
     * @param params 设备事件信息参数
     * @return
     */
    @PostMapping("/addDEventInfo")
    public Result addDEventInfo(
            @RequestHeader("token") String token,
            @RequestBody JSONObject params) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if(null != findDEventInfoByIdOrCode(token,params.getString("deventCode")).getData()){
                return result.build(null, Code.D411);
            }
            params.put("deventId", UUID.randomUUID().toString().replace("-", ""));
            params.put("deventArg", StringUtils.replaceBlank(params.get("deventArg").toString()));
            deviceEventService.addDEventInfo(params);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 根据设备事件ID或设备事件标识查询设备事件信息
     *
     * @param token
     * @param params 设备事件ID或设备事件标识
     * @return
     */
    @GetMapping("/findDEventInfoByIdOrCode/{params}")
    public Result findDEventInfoByIdOrCode(
            @RequestHeader("token") String token,
            @PathVariable("params") String params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        DeviceEvent deviceEvent;
        try{
            deviceEvent = deviceEventService.findDEventInfoByIdOrCode(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(deviceEvent, Code.S200);
    }

    /**
     * 修改设备事件信息
     *
     * @param token
     * @param params 设备事件信息参数
     * @return
     */
    @PutMapping("/updateDEventInfo/{oldDEventCode}")
    public Result updateDEventInfo(
            @RequestHeader("token") String token,
            @PathVariable("oldDEventCode") String oldDEventCode,
            @RequestBody JSONObject params){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            if (!(oldDEventCode.equals(params.get("deventCode")))){
                if(null != findDEventInfoByIdOrCode(token,params.getString("deventCode")).getData()){
                    return result.build(null, Code.D411);
                }
            }
            params.put("deventArg", StringUtils.replaceBlank(params.get("deventArg").toString()));
            deviceEventService.updateDEventInfo(params);
            params.put("oldDEventCode",oldDEventCode);
            deviceEventService.updateDeviceModelArg(params);
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     *删除设备事件信息
     *
     * @param token
     * @param deventId 设备事件ID
     * @return
     */
    @DeleteMapping("/deleteDEventInfo/{deventId}/{deventCode}")
    public Result deleteDEventInfo(
            @RequestHeader("token") String token,
            @PathVariable("deventId") String deventId,
            @PathVariable("deventCode") String deventCode) {
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        try {
            int total = deviceEventService.countEventUseTotal(deventCode);
            if (total > 0){
                return result.build(null,Code.WARM);
            }
            deviceEventService.deleteDEventInfo(deventId);
        } catch (Exception e) {
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(null, Code.S200);
    }

    /**
     * 查询事件类型下拉框
     *
     * @param token
     * @return
     */
    @GetMapping("/findDEventTypeList")
    public Result findDEventTypeList(@RequestHeader("token") String token){
        Result result = new Result();
        if (!tokenService.validate(token)) {
            return result.build(null,Code.D404);
        }
        List<SysCode> deventTypeList;
        try{
            deventTypeList = deviceEventService.findDEventTypeList();
        } catch (Exception e){
            e.printStackTrace();
            return result.build(null,Code.D400);
        }
        return result.build(deventTypeList, Code.S200);
    }

}
